package com.ego.common.util;

import com.ego.common.constant.SystemConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: ego
 * @description: String工具类
 * @author: KamTang
 * @create: 2022-12-09 09:42
 **/
public class StringUtil {
    /**
     * 从source字符串中获取target之后的字符串，类似于获取路径中的参数
     * 例如：source = "attrs=1_2022&attrs=7_第一代骁龙8+ 4G"
     * target = "attrs="
     * 返回：[1_2022, 7_第一代骁龙8+ 4G]
     *
     * @param source 源字符串
     * @param target 目标字符串
     * @return 字符串数组
     */
    public static List<String> getAttrs(String source, String target) {
        if (source == null || target == null) {
            throw new NullPointerException();
        }
        int i = 0;
        int sLen = source.length();
        int tLen = target.length();
        List<String> res = new ArrayList<>();
        while (i < sLen) {
            int j = 0;
            while (j < tLen && source.charAt(i) == target.charAt(j) && i + tLen < sLen) {
                i++;
                j++;
            }
            if (j == tLen) {
                // 开始取值
                StringBuilder stringBuilder = new StringBuilder();
                while (i < sLen && source.charAt(i) != SystemConstant.PARAMETER_CONNECTION_SYMBOL.charAt(0)) {
                    stringBuilder.append(source.charAt(i));
                    i++;
                }
                res.add(stringBuilder.toString());
            }
            i++;
        }
        return res;
    }
}
